//package com.ganzi.manager.util;
//
//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.JSONObject;
//import org.apache.http.client.HttpClient;
//import org.apache.http.client.config.RequestConfig;
//import org.apache.http.client.model.UrlEncodedFormEntity;
//import org.apache.http.client.methods.CloseableHttpResponse;
//import org.apache.http.client.methods.HttpGet;
//import org.apache.http.client.methods.HttpPost;
//import org.apache.http.conn.ssl.NoopHostnameVerifier;
//import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
//import org.apache.http.model.StringEntity;
//import org.apache.http.impl.client.CloseableHttpClient;
//import org.apache.http.impl.client.HttpClients;
//import org.apache.http.message.BasicNameValuePair;
//import org.apache.http.util.EntityUtils;
//
//import javax.net.ssl.SSLContext;
//import javax.net.ssl.TrustManager;
//import javax.net.ssl.X509TrustManager;
//import java.nio.charset.Charset;
//import java.security.cert.CertificateException;
//import java.security.cert.X509Certificate;
//import java.util.ArrayList;
//import java.util.Hashtable;
//import java.util.List;
//
//public class Httpclient {
//public  static final int TimeOut = 30000;
//
//    public static String post(String url, Hashtable<String, String> params) throws Exception {
//        CloseableHttpClient httpClient = null;
//        HttpPost httpPost = null;
//        String result = "";
//        try {
//            httpClient = HttpClients.createDefault();
//            RequestConfig requestConfig = RequestConfig.custom()
//                    .setSocketTimeout(TimeOut).setConnectTimeout(TimeOut).build();
//            httpPost = new HttpPost(url);
//            httpPost.setConfig(requestConfig);
//            List<NameValuePair> ps = new ArrayList<NameValuePair>();
//            for (String pKey : params.keySet()) {
//                ps.add(new BasicNameValuePair(pKey, params.get(pKey)));
//            }
//            httpPost.setEntity(new UrlEncodedFormEntity(ps, "utf-8"));
//            CloseableHttpResponse response = httpClient.execute(httpPost);
//            HttpEntity httpEntity = response.getEntity();
//
//            result = EntityUtils.toString(httpEntity, "utf-8");
//
//        } finally {
//            if (httpPost != null) {
//                httpPost.releaseConnection();
//            }
//            if (httpClient != null) {
//                httpClient.close();
//            }
//        }
//        return result;
//    }
//
//    /**
//     * @Description 处理http请求的get方法
//     * @param url
//     *            :url
//     * @param params
//     *            :参数
//     * @return 返回的字符串
//     */
//    public static String get(String url, Hashtable<String, String> params) throws Exception {
//        CloseableHttpClient httpClient = null;
//        HttpGet httpGet = null;
//
//        String result = "";
//        try {
//            httpClient = HttpClients.createDefault();
//            RequestConfig requestConfig = RequestConfig.custom()
//                    .setSocketTimeout(TimeOut).setConnectTimeout(TimeOut).build();
//            String ps = "";
//            for (String pKey : params.keySet()) {
//                if (!"".equals(ps)) {
//                    ps = ps + "&";
//                }
//                // 处理特殊字符，%替换成%25，空格替换为%20，#替换为%23
//                String pValue = params.get(pKey).replace("%", "%25")
//                        .replace(" ", "%20").replace("#", "%23");
//                ps += pKey + "=" + pValue;
//            }
//            if (!"".equals(ps)) {
//                url = url + "?" + ps;
//            }
//            httpGet = new HttpGet(url);
//            httpGet.setConfig(requestConfig);
//            CloseableHttpResponse response = httpClient.execute(httpGet);
//            HttpEntity httpEntity = response.getEntity();
//            result = EntityUtils.toString(httpEntity, "utf-8");
//        }  finally {
//            if (httpGet != null) {
//                httpGet.releaseConnection();
//            }
//            if (httpClient != null) {
//                httpClient.close();
//            }
//        }
//        return result;
//    }
//
//    /**
//     * @Description 处理https请求的post方法
//     * @param url
//     *            :url
//     * @param params
//     *            :参数
//     * @return 返回的字符串
//     */
//    public static String postSSL(String url, Hashtable<String, String> params) throws Exception {
//        CloseableHttpClient httpClient = null;
//        HttpPost httpPost = null;
//        String result = "";
//        try {
//            httpClient = (CloseableHttpClient) wrapClient();
//            RequestConfig requestConfig = RequestConfig.custom()
//                    .setSocketTimeout(TimeOut).setConnectTimeout(TimeOut).build();
//            httpPost = new HttpPost(url);
//            httpPost.setConfig(requestConfig);
//            List<NameValuePair> ps = new ArrayList<NameValuePair>();
//            for (String pKey : params.keySet()) {
//                ps.add(new BasicNameValuePair(pKey, params.get(pKey)));
//            }
//            httpPost.setEntity(new UrlEncodedFormEntity(ps, "utf-8"));
//            CloseableHttpResponse response = httpClient.execute(httpPost);
//            HttpEntity httpEntity = response.getEntity();
//
//            result = EntityUtils.toString(httpEntity, "utf-8");
//
//        }  finally {
//            if (httpPost != null) {
//                httpPost.releaseConnection();
//            }
//            if (httpClient != null) {
//                httpClient.close();
//            }
//        }
//        return result;
//    }
//
//    /**
//     * @Description 处理https请求的post方法
//     * @param url
//     *            :url
//     * @param stringEntity
//     *            :参数
//     * @return 返回的字符串
//     */
//    public static String postSSL(String url, String stringEntity) throws Exception {
//        System.out.println("--------:"+url);
//        CloseableHttpClient httpClient = null;
//        HttpPost httpPost = null;
//        String result = "";
//        try {
//            httpClient = (CloseableHttpClient) wrapClient();//HttpClients.createDefault();
//            RequestConfig requestConfig = RequestConfig.custom()
//                    .setSocketTimeout(TimeOut).setConnectTimeout(TimeOut).build();
//            httpPost = new HttpPost(url);
//            httpPost.setConfig(requestConfig);
//            //httpPost.setHeader("ContentType", "application/json; charset=utf-8");
//
//            StringEntity model = new StringEntity(stringEntity, Charset.forName("UTF-8"));
//            model.setContentEncoding("UTF-8");
//            // 发送Json格式的数据请求
//            model.setContentType("application/json");
//            httpPost.setEntity(model);
//            HttpResponse response = httpClient.execute(httpPost);
//            response.setHeader("Content-Type", "text/plain;charset=UTF-8");
//            StatusLine statusLine = response.getStatusLine();
//            if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
//                HttpEntity httpEntity = response.getEntity();
//                result = EntityUtils.toString(httpEntity, "utf-8");
//            } else {
//                JSONObject info = (JSONObject) JSON.parse(statusLine.toString());
//                throw new Exception("请求出错，StatusLine：" + info.toJSONString());
//            }
//        } finally {
//            if (httpPost != null) {
//                httpPost.releaseConnection();
//            }
//            if (httpClient != null) {
//                httpClient.close();
//            }
//        }
//        return result;
//    }
//
//    /**
//     * @Description 处理https请求的get方法
//     * @param url
//     *            :url
//     * @param params
//     *            :参数
//     * @return 返回的字符串
//     */
//    public static String getSSL(String url, Hashtable<String, String> params) throws Exception {
//        CloseableHttpClient httpClient = null;
//        HttpGet httpGet = null;
//        String result = "";
//        try {
//            httpClient = (CloseableHttpClient) wrapClient();
//            RequestConfig requestConfig = RequestConfig.custom()
//                    .setSocketTimeout(TimeOut).setConnectTimeout(TimeOut).build();
//            String ps = "";
//            for (String pKey : params.keySet()) {
//                if (!"".equals(ps)) {
//                    ps = ps + "&";
//                }
//                // 处理特殊字符，%替换成%25，空格替换为%20，#替换为%23
//                String pValue = params.get(pKey).replace("%", "%25")
//                        .replace(" ", "%20").replace("#", "%23");
//                ps += pKey + "=" + pValue;
//            }
//            if (!"".equals(ps)) {
//                url = url + "?" + ps;
//            }
//            httpGet = new HttpGet(url);
//            httpGet.setConfig(requestConfig);
//            CloseableHttpResponse response = httpClient.execute(httpGet);
//            HttpEntity httpEntity = response.getEntity();
//            result = EntityUtils.toString(httpEntity, "utf-8");
//        } finally {
//            if (httpGet != null) {
//                httpGet.releaseConnection();
//            }
//            if (httpClient != null) {
//                httpClient.close();
//            }
//        }
//        return result;
//    }
//
//    /**
//     * @Description 创建一个不进行正式验证的请求客户端对象 不用导入SSL证书
//     * @return HttpClient
//     */
//    public static HttpClient wrapClient() {
//        try {
//            SSLContext ctx = SSLContext.getInstance("TLS");
//            X509TrustManager tm = new X509TrustManager() {
//                public X509Certificate[] getAcceptedIssuers() {
//                    return null;
//                }
//                public void checkClientTrusted(X509Certificate[] arg0,
//                                               String arg1) throws CertificateException {
//                }
//                public void checkServerTrusted(X509Certificate[] arg0,
//                                               String arg1) throws CertificateException {
//                }
//            };
//            ctx.init(null, new TrustManager[] { tm }, null);
//            SSLConnectionSocketFactory ssf = new SSLConnectionSocketFactory(
//                    ctx, NoopHostnameVerifier.INSTANCE);
//            CloseableHttpClient httpclient = HttpClients.custom()
//                    .setSSLSocketFactory(ssf).build();
//            return httpclient;
//        } catch (Exception e) {
//            return HttpClients.createDefault();
//        }
//    }
//}